  function out_rcdraws( xmat , oname , in ); 
% =====================================================
% function out_rcdraws ( xmat , oname , in ); 
% 
% Create an output table for a matrix of dimensions 
% xmat  [ nz x nx x ndr ]
% 
% Table will look at one row at a time and store the output 
% in different columns 
% Use for variance decompositions for instance 
% Use OUT_CTDRAWS  for time / calendar draws 
%
% IN.CNAMES 
% IN.RNAMES 
% IN.TOPROW         Column Cell before title (e.g. PATH and DATASET) 
% IN.TITLE          Of table
% IN.TOPCELL        Cell Below the Title 
% IN.OUTPATH 
% 
% 
% Alejandro Justiniano 7/27/2005 
% ======================================================

% Use the following example to ensure elements are positioned 
% corectly in the output matrix  
% A=[10 -1  40 -2 60 -3];
% A= repmat( A , [200 1] );
% B = randn(200,6)*( 0.01*eye(6) );
% A = (A + B)' ;
% xmat = reshape( A , [2 3 200] );
cucd = cd; 
if nargin < 3; 
    in.title = {'Title'}; 
end 
dim=size(xmat); 
if length(dim)~= 3 
    error('XMAT must be 3D') 
end 
nr=dim(1); 
nc=dim(2); 
ndr=dim(3); 
cperc=7;  % Columns of output per column of input 

% -----------------------------------------
%    Default Settings 
% -----------------------------------------
[e,in]=ch_field(in,'cnames'); 
if e==0 
    in.cnames=fnumcell('col ',[1:nc]); 
else 
    if length( in.cnames ) ~= nc 
        error('Input in.CNAMES does not match number of columns') 
    end 
end 
[e,in]=ch_field(in,'rnames'); 
if e==0 
    in.rnames = fnumcell('row ',[1:nr]);
    if length(in.rnames)~=nr
        error('Input in.RNAMES does not match number of rows')
    end
end 
[e,in]=ch_field( in, 'title' ,'Series' ); 
[e,in]=ch_field( in, 'topcell', {'Table'} ); 
[e,in]=ch_field( in, 'toprow' ); 
if e == 0 
    flag_topr = 0; 
else 
    if iscell( in.toprow ) == 0 
        in.toprow = cellstr( in.toprow ); 
    end 
    flag_topr = 1; 
end 
[e,in]=ch_field(in,'outpath',cucd); 
%----------------------------------------------
%               Percentiles 
% ---------------------------------------------
perc=[0.05 ; 0.16];
pmat=[0.05  0.16; 0.95  0.84];
clear perc*; 
pmat=pmat(:); 
pcell=num2cprec(100*pmat)'; 
exind=round(ndr*pmat); 
exind=exind(:); 
% --------------------------
%     Output cell 
% --------------------------
% Matrix indicator, one column per column of output 
indmat=( reshape( 2:(cperc*nc+1) ,[cperc nc] ) ); 
ncell=length(in.topcell); 
ntop=max(ncell,2)+5; 

dimc =  length( indmat(:) ) + 1 ; 
dimr = ntop + nr; 

ocell = emptycell( dimr , dimc ); 

% Top cells 
% ===========
ocell(1:ncell,1) = in.topcell ; 
ocell(ncell+1,1) =  cellstr( in.title ); 
ocell(ncell+1,2:3) = {'File name:' ,oname }; 
% Column Names 
temp= repmat( in.cnames(:)' , [cperc 1] ); 
ocell(ncell+2,2:end) = ( temp(:) )'; 
ocell(ncell+3,2:end) =  fillcell(1,dimc-1,'====='); 

temp= { 'Mean','Median','Std','5%','95%','16%','84%' }; 
ocell(ncell+4,2:end) = repmat( temp , [1 nc] ); 
clear ncell temp; 

% Write output, one row at a time 
% ================================

hh=1; 
for hh=1:nr; 
    
    ocell( ntop + hh , 1 ) = in.rnames(hh); 
    
    temp = ( reshape( xmat(hh,:,:) ,[nc ndr] ) )'; 
    
    ocell( ntop+ hh  , indmat(1,:) ) = num2cprec( mean(temp ) ); 
    ocell( ntop+ hh  , indmat(2,:) ) = num2cprec( median(temp) ); 
    ocell( ntop+ hh  , indmat(3,:) ) = num2cprec( std(temp) ) ; 
   
    temp = sort( temp );
    ii=1; 
    for ii=1:4;
       ocell( ntop+hh , indmat(3+ii,:) ) = num2cprec( temp( exind(ii) , : ) );
    end

end 
if flag_topr == 1 
    ocell = merge_cells( in.toprow, ocell , 1 ); 
end 

savecell( ocell , [], in.outpath , oname ); 
disp(['Saved ocell in path ',in.outpath ] ); 
